package com.fenghongzhang.day006;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import java.util.List;

public class MyAdapter extends PagerAdapter{
    private List<String> list;
    private Context context;

    public MyAdapter(List<String> list, Context context) {
        this.list = list;
        this.context = context;
    }

    //总页数
    @Override
    public int getCount() {
        return list.size();
    }

    //返回视图是不是指定的.
    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    //初始化每一页
    //getView 每一行
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {

        //加载布局,并返回布局
        View inflate = LayoutInflater.from(context).inflate(R.layout.wel_layout, null);

        TextView textView = inflate.findViewById(R.id.wel_text);
        textView.setText(list.get(position));

        //必须有
        container.addView(inflate);

        return inflate;
    }

    //不用的时候,移除
    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
//        super.destroyItem(container, position, object);//可能报错.
        container.removeView((View)object);
    }
}
